# -*- coding:utf8 -*-

import time

from common import singleton, draw_text
from logger import LOG


@singleton
class Fps(object):
    def __init__(self, pos_x=10, pos_y=18):
        self.__last_ms = 0
        self._frames = 0
        self.fps = 0
        self.pos_x = pos_x
        self.pos_y = pos_y

    def get(self):
        mile_second = time.clock()

        if mile_second - self.__last_ms >= 1.0:
            self.__last_ms = mile_second
            self.fps = self._frames
            self._frames = 0
            LOG.debug('fps: %d', self.fps)
            LOG.debug('time: %s', time.ctime())
        self._frames += 1
        return self.fps

    def show(self):
        fps = self.get()
        draw_text("FPS: " + str(fps), self.pos_x, self.pos_y)